package com.zr.corporatefactory.controller;

import com.zr.corporatefactory.model.User;
import com.zr.corporatefactory.model.UserLogin;
import com.zr.corporatefactory.service.UserService;
import com.zr.qianduan.ResultVOBuilder;
import com.zr.util.ErrorMessageClass;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 天赐 on 2019/12/25.
 */
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public ResultVo login(@RequestBody UserLogin userLogin, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }

        User user = userService.login(userLogin);
        return ResultVoBuilder.success(user);
    }
}
